package weaver.interfaces.htsc.duss.action;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-09-24
 * @Description: 申请节点提交后删除明细表数据
 * @ModifyLog:
 **/

public class WeaClearDtlTblAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        String requestid = requestInfo.getRequestid();
        writeLog( "----WeaClearDtlTblAction-------" + requestid );
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String sql = "select id,hqbm,kgsldsp,sblb,sblx from " + tablename + " where requestid='" + requestid + "'";
        writeLog( sql );
        try {
            rs.executeQuery( sql );
            rs.next();
            String sblb = Util.null2String( rs.getString( "sblb" ) );//申报类别
            String sblx = Util.null2String( rs.getString( "sblx" ) );//申报类型
            String mainid = Util.null2String( rs.getString( "id" ) );//mainid
            String hqbms = Util.null2String( rs.getString( "hqbm" ) );//会签部门
            String kgsldsps = Util.null2String( rs.getString( "kgsldsp" ) );//跨公司会签
            String sql1 = "select wm_concat(bjhqbm) as bjhqbm from uf_zysxsyfw where sblb ='" + sblb + "' and sblx='" + sblx + "'";
            rs2.executeQuery( sql1 );
            String bjhqbm = "";
            if (rs2.next()) {
                bjhqbm = Util.null2String( rs2.getString( "bjhqbm" ) );//必经会签部门
            }
            writeLog("必经会签部门："+bjhqbm);
            if (!"".equals( hqbms )) {
                rs.executeUpdate( "delete " + tablename + "_dt2 where mainid='" + mainid + "'" );
                String[] arr = hqbms.split( "," );
                for (String hqbm : arr) {
                    int isBjhqbm =0;
                    if(bjhqbm.contains(hqbm)){
                        isBjhqbm=1;
                    }
                    rs1.executeQuery( "select deptdirectors from hrmdepartmentdefined where deptid='" + hqbm + "'" );
                    rs1.next();
                    String deptdirectors = Util.null2String( rs1.getString( "deptdirectors" ) );
                    String insert_sql = "";
                    if (deptdirectors.split( "," ).length >= 2 || "180".equals( hqbm )) {
                        insert_sql = "insert into " + tablename + "_dt2 (mainid,hqbm,bshq)values('" + mainid + "','" + hqbm + "','"+isBjhqbm+"')";
                    } else {
                        insert_sql = "insert into " + tablename + "_dt2 (mainid,hqbm,bmfzr,bshq)values('" + mainid + "','" + hqbm + "','" + deptdirectors + "','"+isBjhqbm+"')";
                    }

                    rs.executeUpdate( insert_sql );
                    writeLog( insert_sql );
                }
            }
            if (!"".equals( kgsldsps )) {
                rs.executeUpdate( "delete " + tablename + "_dt3 where mainid='" + mainid + "'" );
                String[] arr = kgsldsps.split( "," );
                for (String kgsldsp : arr) {
                    String insert_sql = "insert into " + tablename + "_dt3 (mainid,bmfzr,sfhq)values('" + mainid + "','" + kgsldsp + "','0')";
                    rs.executeUpdate( insert_sql );
                    writeLog( insert_sql );
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
